#include <ie.au3>

Opt("WinTitleMatchMode", 2)
; NOW SET FOR GENERIC

WinSetState( "C:\WINDOWS\system32\cmd.exe", "", @SW_MINIMIZE )

	; Create a hidden IE window
dim $objPolicy = _IECreate( "http://127.0.0.1:9595/policy.cgi.exe", 0, 0 )
	; Put the HTML body in a string
dim $txtPolicyBody = _IEBodyReadHTML( $objPolicy )
	; Initialize rest of vars
dim $currentChar, $i = 0
dim $indexID, $pwrID, $chrTest, $pwrIndex ;, $sessionID, $sessionIndex
dim $synID, $synIndex

dim $pwrTime = TimerInit()
dim $mainForm = _IEFormGetObjByName( $objPolicy, "mainForm" )
dim $refreshButton = _IEFormElementGetObjByName( $mainForm, "download" )

; Power config ------------------------------------------------------------------------------------

Do
	; Find the index "Power Configuration" is at
	$pwrIndex = StringInStr( $txtPolicyBody, "Power Configuration" )

	; display 'Power Configuration' index if found
	if $pwrIndex Then
		TrayTip( "", "Power Configuration policy found at index " & $pwrIndex & ".", 5 )
	Else
		; Refresh window and wait 5 seconds if power config hasn't been pulled
		TrayTip( "Power Configuration", "Waiting for power config to pull... " & timeFormat(TimerDiff($pwrTime)), 5 )
		_IEFormSubmit( $refreshButton )
		Sleep(5000)
	EndIf

	; Exit loop when the index is found
Until $pwrIndex

; Do a bunch of tedious string searching backwards from the power config index
do
	$currentChar = StringMid( $txtPolicyBody, $pwrIndex-$i, 1 )
	$i += 1
Until StringIsDigit( $currentChar )
$indexID = $pwrIndex-$i

$i = 0

; When the ID end index is found, search backwards until a non-digit is found, then load that ID into $pwrID
do
	$currentChar = StringMid( $txtPolicyBody, $indexID-$i, 1 )
	$i += 1
Until NOT StringIsDigit( $currentChar )

$pwrID = StringMid( $txtPolicyBody, ($indexID-$i)+2, $i )

TrayTip( "", "Power config policy ID: " & $pwrID & ".", 5 )

dim $pwrButton = _IEFormElementGetObjByName( $mainForm, $pwrID )

; Launch power config policy ID when found
_IENavigate( $objPolicy, "http://127.0.0.1:9595/policy.cgi.exe?action=install&sessionId=&policyId=" & $pwrID )
;ShellExecute( "http://127.0.0.1:9595/policy.cgi.exe?action=install&sessionId=&policyId=" & $pwrID, "", "", "", @SW_HIDE )
sleep(2000)

; Initialize a bunch of dumb variables
Dim $compIsLaptop, $QS
Const $TS = Chr(9) & Chr(32)
Local $winTitle = "Power Options Properties"
if FileExists( @DesktopCommonDir & "\QPM.lnk" ) Then
	$QS = 1
EndIf

winwaitactive( $winTitle )

;Power Schemes

$compIsLaptop = ControlFocus( $winTitle, "Select the power scheme", 1013 )

if $compIsLaptop Then
	TrayTip( "", "Setting power for laptop", 5 )
	; Set Power Schemes options for laptop
	ControlCommand( $winTitle, "Select the power scheme", 1000, "SelectString", 'Max Battery' )
	ControlCommand( $winTitle, "Select the power scheme", 1007, "SelectString", 'After 15 mins' )
	ControlCommand( $winTitle, "Select the power scheme", 1008, "SelectString", 'After 15 mins' )
	ControlCommand( $winTitle, "Select the power scheme", 1016, "SelectString", 'After 25 mins' )
	ControlCommand( $winTitle, "Select the power scheme", 1017, "SelectString", 'After 25 mins' )
	ControlCommand( $winTitle, "Select the power scheme", 1005, "SelectString", 'Never' )
	ControlCommand( $winTitle, "Select the power scheme", 1006, "SelectString", 'Never' )
	; Apply
	ControlClick( $winTitle, "Select the power scheme", 12321 )

	; Tab to Advanced settings
	ControlCommand( $winTitle, "", 12320, "TabRight", "" )
	sleep(500)
	ControlCommand( $winTitle, "", 12320, "TabRight", "" )
	sleep(500)
	ControlCommand( $winTitle, "", 12320, "TabRight", "" )
	sleep(500)
ElseIf $QS Then
	TrayTip( "", "Setting power for QS workstation", 5 )
	; Set Power Schemes options for desktop
	ControlCommand( $winTitle, "Select the power scheme", 1000, "SelectString", 'Presentation' )
	ControlCommand( $winTitle, "Select the power scheme", 1007, "SelectString", 'Never' )
	ControlCommand( $winTitle, "Select the power scheme", 1016, "SelectString", 'Never' )
	ControlCommand( $winTitle, "Select the power scheme", 1005, "SelectString", 'Never' )
	; Apply
	ControlClick( $winTitle, "Select the power scheme", 12321 )

	; Tab to Advanced settings
	ControlCommand( $winTitle, "", 12320, "TabRight", "" )
	sleep(500)

Else
	TrayTip( "", "Setting power for desktop", 5 )
	; Set Power Schemes options for desktop
	ControlCommand( $winTitle, "Select the power scheme", 1000, "SelectString", 'Max Battery' )
	; Monitor
	ControlCommand( $winTitle, "Select the power scheme", 1007, "SelectString", 'After 15 mins' )
	; HDD
	ControlCommand( $winTitle, "Select the power scheme", 1016, "SelectString", 'After 25 mins' )
	sleep(250)
	; Sleep
	ControlCommand( $winTitle, "Select the power scheme", 1005, "SelectString", 'Never' )
	; Apply
	ControlClick( $winTitle, "Select the power scheme", 12321 )

	; Tab to Advanced settings
	ControlCommand( $winTitle, "", 12320, "TabRight", "" )
	sleep(500)
EndIf

; Set Advanced settings

; Ask for password
ControlCommand( $winTitle, "Select the power-saving", 1307, "Uncheck", "" )
; Power button behavior
ControlCommand( $winTitle, "Select the power-saving", 1303, "SelectString", 'Ask me what to do' )
; Apply
ControlClick( $winTitle, "Select the power-saving", 12321 )

; Tab to Hibernate options
ControlCommand( $winTitle, "", 12320, "TabRight", "" )
sleep(500)

; Uncheck hibernation
ControlCommand( $winTitle, "When your computer hibernates", 1400, "Uncheck", "" )
; Apply
ControlClick( $winTitle, "When your computer hibernates", 12321 )
; OK
ControlClick( $winTitle, "When your computer hibernates", 1 )

TrayTip("", "Power config complete", 5 )

; Synapse settings ------------------------------------------------------------------------------------
#cs
$synTime = TimerInit()

Do
	; Find the index "Fuji Clinical Workstation Setttings" is at
	$synIndex = StringInStr( $txtPolicyBody, "Fuji Clinical Workstation Setttings" )

	; display 'Fuji Clinical Workstation Setttings' index if found
	if $synIndex Then
		TrayTip( "postInstall", "Synapse clinical settings found at index " & $pwrIndex & ".", 5 )
	Else
		; Refresh window and wait 5 seconds if power config hasn't been pulled
		TrayTip( "postInstall", "Waiting for synapse settings to pull... " & timeFormat(TimerDiff($synTime)), 5 )
		_IEAction( $objPolicy, "refresh" )
		Sleep(5000)
	EndIf

	; Exit loop when the index is found
Until $synIndex

; Do a bunch of tedious string searching backwards from the index
$i = 0
do
	$currentChar = StringMid( $txtPolicyBody, $synIndex-$i, 1 )
	$i += 1
Until StringIsDigit( $currentChar )
$indexID = $synIndex-$i

$i = 0

; When the ID end index is found, search backwards until a non-digit is found, then load that ID into $synID
do
	$currentChar = StringMid( $txtPolicyBody, $indexID-$i, 1 )
	$i += 1
Until NOT StringIsDigit( $currentChar )

$synID = StringMid( $txtPolicyBody, ($indexID-$i)+2, $i )

TrayTip( "", "Synapse clinical settings policy ID: " & $synID & ".", 5 )

dim $synButton = _IEFormElementGetObjByName( $mainForm, $synID )

; Launch power config policy ID when found
_IENavigate( $objPolicy, "http://127.0.0.1:9595/policy.cgi.exe?action=install&sessionId=&policyId=" & $synID )

WinWait( "Reboot Required" )

WinClose( "Reboot Required" )
#ce

; Close SDP window
_IEAction( $objPolicy, "quit" )

if WinExists( "Software Deployment Portal" ) Then WinClose( "Software Deployment Portal" )

sleep(3000)

; Delete dupe AA icon and misc. icons if found
if FileExists( @DesktopDir & "\*AccessAnyware.lnk") Then FileDelete( @desktopdir & "\*AccessAnyware.lnk" )


; delete logoff shortcut if found
if FileExists( @desktopdir & "\logoff.lnk" ) Then FileDelete(@desktopdir & "\logoff.lnk")

; Test , make icons, launch inv & AA, etc
dim $timeInit = TimerInit()

Do
	sleep(100)
	TrayTip( "REMOVED", "Waiting for REMOVED to pull..." & @CRLF & "	" & timeFormat(TimerDiff($timeInit)), 1 )
Until FileExists( @DesktopDir & "\Citrix Applications\REMOVED.lnk" ) OR TimerDiff($timeInit) > 15000

if NOT FileExists( @DesktopDir & "\REMOVED.lnk" ) Then
	FileCopy( @DesktopDir & "\Citrix Applications\REMOVED.lnk", @DesktopDir & "\ REMOVED.lnk" )
ElseIf NOT StringInStr( FileGetShortcut( @DesktopDir & "\ REMOVED.lnk" ), "pnagent.exe" ) Then
	FileDelete( @DesktopDir & "\ REMOVED.lnk" )
	FileCopy( @DesktopDir & "\Citrix Applications\REMOVED.lnk", @DesktopDir & "\ REMOVED.lnk" )
EndIf

FileDelete( @DesktopDir & "\REMOVED.lnk" )

FileDelete( @desktopdir & "\REMOVED - Demo.lnk" )
FileCopy( @DesktopDir & "\Citrix Applications\REMOVED - Demo.lnk", @DesktopDir & "\REMOVED - Demo.lnk" )

dim $ccLaunch = ShellExecute( @DesktopDir & "\ REMOVED.lnk" )

if $ccLaunch Then
	WinWaitActive( "REMOVED", "", 30 )
	TrayTip( "", "REMOVED launched and working", 5 )
	sleep(2000)
	Send( "!x" )
EndIf

Dim $srcInv, $srcAA

$srcInv = FileFindFirstFile( @DesktopCommonDir & "\INVISION*" )
ShellExecute( @DesktopCommonDir & "\" & FileFindNextFile($srcInv) )
$srcAA = FileFindFirstFile( @DesktopCommonDir & "\*accessANYware.lnk" )
ShellExecute( @DesktopCommonDir & "\" & fileFindNextFile($srcAA) )

FileClose($srcInv)
FileClose($srcAA)

; Inventory Scan
ShellExecute( @StartMenuCommonDir & "\Programs\LANDesk Management\Inventory Scan.lnk" )
WinWaitActive( "Inventory Scanner", "", 15 )
WinWaitClose( "Inventory Scanner", "" )
traytip( "", "Inventory Scan Complete" & @CRLF & @CRLF & "Checking IE version...", 5 )

dim $ieVersion = FileGetVersion( @programfilesdir & "\Internet Explorer\iexplore.exe" )
if StringLeft( $ieVersion, 1 ) < 7 Then
	MsgBox(48, $ieVersion, "Internet Explorer version outdated, install IE7" )
Else
	TrayTip( $ieVersion, "Internet Explorer up-to-date.", 5 )
EndIf

; resolution via qRes
$qresInst = FileInstall( "qres.exe", @desktopdir & "\qres.exe" )
if $qresInst Then
	$qresState = ShellExecuteWait( "qres.exe", "/x 1024 /y 768" )
	FileDelete( @desktopdir & "\qres.exe" )
	if $qresState Then TrayTip( "Resolution", "Successfully set standard resolution.", 3 )
Else
	TrayTip( "Resolution", "Problem copying qres.", 3 )
EndIf

; gpupdate
TrayTip( "gpupdate", "Updating group policy...", 3 )
ShellExecute( "gpupdate", "/force" )
Sleep(10000)
WinClose( "gpupdate" )

exit

func timeFormat($ms)
	dim $sec, $min, $hr
	dim $s = round($ms/1000, 0 )

	$sec = mod($s, 60)
	if $sec < 10 then $sec = "0" & $sec
	$min = Floor(mod($s/60, 60))
	if $min < 10 then $min = "0" & $min
	$hr = Floor($s/3600)
	if $hr < 10 then $hr = "0" & $hr

	Return $hr & ":" & $min & ":" & $sec

EndFunc
